package com.spider.admin.controller;

import com.spider.admin.exception.SpiderException;
import com.spider.admin.http.entity.PageResult;
import com.spider.admin.service.service.Service;
import com.spider.admin.store.primary.model.ServiceInfo;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
@Validated
@RequestMapping("/service")
public class ServiceController {
    @Autowired
    Service service;

    @GetMapping("/list/{tenantId}")
    public PageResult<ServiceInfo> queryServiceByTenantId(
            @PathVariable("tenantId") @NotEmpty(message = "ID参数不能为空") String tenantId,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String serviceName,
            @RequestParam(required = false) String appId) throws SpiderException {
        return service.queryServiceByTenantId(page, pageSize, serviceName, tenantId, null, appId);
    }

    @GetMapping("/allList")
    public PageResult<ServiceInfo> queryServiceAllList(
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String serviceName,
            @RequestParam(required = false) String curTenantId,
            @RequestParam(required = false) String tenantId,
            @RequestParam(required = false) String appId
    ) throws SpiderException {
        return service.queryServiceByTenantId(page, pageSize, serviceName, curTenantId, tenantId, appId);
    }

    @GetMapping("/query/{pid}")
    public ServiceInfo queryServiceById(@PathVariable("pid") @NotEmpty(message = "ID参数不能为空") String pid) throws SpiderException {
        return service.queryServiceById(pid);
    }

    @PostMapping("/add")
    public int saveServiceInfo(@RequestBody @Validated(ServiceInfo.Save.class) ServiceInfo serviceInfo) throws SpiderException {
        return service.saveService(serviceInfo);
    }

    @PutMapping("/update/{pid}")
    public int updateServiceInfo(@RequestBody @Validated(ServiceInfo.Update.class) ServiceInfo serviceInfo, @PathVariable @NotEmpty(message = "ID参数不能为空") String pid) throws SpiderException {
        return service.updateService(serviceInfo, pid);
    }

    @PutMapping("/online/{pid}")
    public int activeService(@PathVariable @NotEmpty(message = "ID参数不能为空") String pid) throws SpiderException {
        return service.onLineService(pid);
    }

    @PutMapping("/offline/{pid}")
    public int archiveService(@PathVariable @NotEmpty(message = "ID参数不能为空") String pid) throws SpiderException {
        return service.offLineService(pid);
    }

    @DeleteMapping("/remove/{pid}")
    public int removeService(@PathVariable @NotEmpty(message = "ID参数不能为空") String pid) throws SpiderException {
        return service.remove(pid);
    }
}

